#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}


double Widget::fun(double x){
    if(x==0) return 1.0;
    return sin(x)/x;
}
void Widget::solve_div(){
    double a = this->ui->doubleSpinBox->value();
    double b = this->ui->doubleSpinBox_2->value();
    int n = this->ui->spinBox->value();
    double tans = 0;

    double h = (b-a)/n;
    double ans = h/2*(fun(a)+fun(b));
    for(int i=1;i<n;i++){
        ans += fun(a+h*i)*h;
    }
    while(ans - tans > 0.00001){
        tans = ans;

        n *= 2;
        h = (b-a)/n;
        ans = h/2*(fun(a)+fun(b));
        for(int i=1;i<n;i++){
            ans += fun(a+h*i)*h;
        }

        ui->plainTextEdit->appendPlainText(QString::number(ans));
    }

}
void Widget::solve_rom(){
    double a = this->ui->doubleSpinBox->value();
    double b = this->ui->doubleSpinBox_2->value();
    int n = this->ui->spinBox->value();
    double ans;
    double h = (b-a)/n;
    val[0][0] = h/2*(fun(a)+fun(b));
    len = 0;
    do{
        len++;
        n *= 2;
        h = (b-a)/n;
        ans = h/2*(fun(a)+fun(b));
        for(int i=1;i<n;i++){
            ans += fun(a+h*i)*h;
        }
        val[len][0] = ans;
    }while(val[len][0]-val[len-1][0]>0.00001);

    for(int i=1;i<len;i++){
        for(int j=1;j<=i;j++){
            val[i][j] = ((1<<(2*j))*val[i][j-1]-val[i-1][j-1])/((1<<(2*j))-1);
        }
    }
    QString Qs = "";
    for(int i=0; i<len; i++){
        for(int j=0; j<=i; j++){
            Qs += QString::number(val[i][j]);
            Qs += '\t';
        }
        Qs += '\n';
    }
    ui->plainTextEdit_2->clear();
    ui->plainTextEdit_2->appendPlainText(Qs);
}

void Widget::on_pushButton_clicked()
{
    solve_div();
    solve_rom();
}
